﻿using DevExpress.XtraGrid.Views.BandedGrid;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Linq;

namespace NursingFormSystem.FallRiskAssessment
{
    public partial class HistoryFallRiskAssessment : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        Tb_User LoginUser;
        PatientInfo PatientInfo;
        #endregion

        #region 窗体函数
        public HistoryFallRiskAssessment(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            LoginUser = loginUser;
            PatientInfo = patientInfo;
            BindControl();
            BindInfo();
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;//住院号
            int VisitId = this.PatientInfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.FallFRA;//12006  //表单ID
            DataTable dt = new DataTable();
            dt.Columns.Add("pingguriqi", typeof(string));
            dt.Columns.Add("nainling1", typeof(string));
            dt.Columns.Add("nainling2", typeof(string));
            dt.Columns.Add("nainling3", typeof(string));
            dt.Columns.Add("nainling4", typeof(string));
            dt.Columns.Add("xingbie1", typeof(string));
            dt.Columns.Add("xingbie2", typeof(string));
            dt.Columns.Add("zhenduan1", typeof(string));
            dt.Columns.Add("zhenduan2", typeof(string));
            dt.Columns.Add("zhenduan3", typeof(string));
            dt.Columns.Add("zhenduan4", typeof(string));
            dt.Columns.Add("renzhizhangai1", typeof(string));
            dt.Columns.Add("renzhizhangai2", typeof(string));
            dt.Columns.Add("renzhizhangai3", typeof(string));
            dt.Columns.Add("huanjingyinsu1", typeof(string));
            dt.Columns.Add("huanjingyinsu2", typeof(string));
            dt.Columns.Add("huanjingyinsu3", typeof(string));
            dt.Columns.Add("huanjingyinsu4", typeof(string));
            dt.Columns.Add("duishoushudanying1", typeof(string));
            dt.Columns.Add("duishoushudanying2", typeof(string));
            dt.Columns.Add("duishoushudanying3", typeof(string));
            dt.Columns.Add("yaowuyingyong1", typeof(string));
            dt.Columns.Add("yaowuyingyong2", typeof(string));
            dt.Columns.Add("yaowuyingyong3", typeof(string));
            dt.Columns.Add("zongdefen", typeof(string));
            dt.Columns.Add("pingguren", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, StatDate.DateTime, EndDate.DateTime);
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                string theTrue = "√";
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    Fallfar fd = new Fallfar();
                    fd = XmlUtil.XMLToObject<Fallfar>(NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    row["pingguriqi"] = Convert.ToDateTime(fd.RiQi).ToString("yyyy-MM-dd");
                    //年龄
                    if (!string.IsNullOrEmpty(fd.NianLing))
                    {
                        string ysgj_yy = fd.NianLing.IndexOf("#") < 0 ? "0#0#0#0" : fd.NianLing;
                        string[] yygj_yyArray = ysgj_yy.Split('#');
                        if (yygj_yyArray.Length >= 4)
                        {
                            row["nainling1"] = yygj_yyArray[0].Equals("1") ? theTrue : string.Empty;
                            row["nainling2"] = yygj_yyArray[1].Equals("1") ? theTrue : string.Empty;
                            row["nainling3"] = yygj_yyArray[2].Equals("1") ? theTrue : string.Empty;
                            row["nainling4"] = yygj_yyArray[3].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    //性别
                    if (!string.IsNullOrEmpty(fd.XingBie))
                    {
                        string ysgj_yf = fd.XingBie.IndexOf("#") < 0 ? "0#0" : fd.XingBie;
                        string[] ysgj_yf_arrary = ysgj_yf.Split('#');
                        if (ysgj_yf_arrary.Length >= 2)
                        {
                            row["xingbie1"] = ysgj_yf_arrary[0].Equals("1") ? theTrue : string.Empty;
                            row["xingbie2"] = ysgj_yf_arrary[1].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    //诊断
                    if (!string.IsNullOrEmpty(fd.ZhenDuan))
                    {
                        string ysgj_bfz = fd.ZhenDuan.IndexOf("#") < 0 ? "0#0#0#0" : fd.ZhenDuan;
                        string[] ysgj_bfz_array = ysgj_bfz.Split('#');
                        if (ysgj_bfz_array.Length >= 4)
                        {
                            row["zhenduan1"] = ysgj_bfz_array[0].Equals("1") ? theTrue : string.Empty;
                            row["zhenduan2"] = ysgj_bfz_array[1].Equals("1") ? theTrue : string.Empty;
                            row["zhenduan3"] = ysgj_bfz_array[2].Equals("1") ? theTrue : string.Empty;
                            row["zhenduan4"] = ysgj_bfz_array[3].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    //认知障碍
                    if (!string.IsNullOrEmpty(fd.RenZhiZhangAi))
                    {
                        string ershisi_ysza = fd.RenZhiZhangAi.IndexOf("#") < 0 ? "0#0#0" : fd.RenZhiZhangAi;
                        string[] ershisi_ysza_Array = ershisi_ysza.Split('#');
                        if (ershisi_ysza_Array.Length >= 3)
                        {
                            row["renzhizhangai1"] = ershisi_ysza_Array[0].Equals("1") ? theTrue : string.Empty;
                            row["renzhizhangai2"] = ershisi_ysza_Array[1].Equals("1") ? theTrue : string.Empty;
                            row["renzhizhangai3"] = ershisi_ysza_Array[2].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    //环境因素
                    if (!string.IsNullOrEmpty(fd.HuanJingYinSu))
                    {
                        string ershisi_jsza = fd.HuanJingYinSu.IndexOf("#") < 0 ? "0#0#0#0" : fd.HuanJingYinSu;
                        string[] ershisi_jsza_array = ershisi_jsza.Split('#');
                        if (ershisi_jsza_array.Length >= 4)
                        {
                            row["huanjingyinsu1"] = ershisi_jsza_array[0].Equals("1") ? theTrue : string.Empty;
                            row["huanjingyinsu2"] = ershisi_jsza_array[1].Equals("1") ? theTrue : string.Empty;
                            row["huanjingyinsu3"] = ershisi_jsza_array[2].Equals("1") ? theTrue : string.Empty;
                            row["huanjingyinsu4"] = ershisi_jsza_array[3].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    //对手术镇静麻醉的反应
                    if (!string.IsNullOrEmpty(fd.DuiShouShuZhenJingMaZuiDeFanYing))
                    {
                        string ershisi_nl = fd.DuiShouShuZhenJingMaZuiDeFanYing.IndexOf("#") < 0 ? "0#0#0" : fd.DuiShouShuZhenJingMaZuiDeFanYing;
                        string[] ershisi_nl_array = ershisi_nl.Split('#');
                        if (ershisi_nl_array.Length >= 3)
                        {
                            row["duishoushudanying1"] = ershisi_nl_array[0].Equals("1") ? theTrue : string.Empty;
                            row["duishoushudanying2"] = ershisi_nl_array[1].Equals("1") ? theTrue : string.Empty;
                            row["duishoushudanying3"] = ershisi_nl_array[2].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    //药物反应
                    if (!string.IsNullOrEmpty(fd.YaoWuYingYong))
                    {
                        string ershisi_nl = fd.YaoWuYingYong.IndexOf("#") < 0 ? "0#0#0" : fd.YaoWuYingYong;
                        string[] ershisi_nl_array = ershisi_nl.Split('#');
                        if (ershisi_nl_array.Length >= 3)
                        {
                            row["yaowuyingyong1"] = ershisi_nl_array[0].Equals("1") ? theTrue : string.Empty;
                            row["yaowuyingyong2"] = ershisi_nl_array[1].Equals("1") ? theTrue : string.Empty;
                            row["yaowuyingyong3"] = ershisi_nl_array[2].Equals("1") ? theTrue : string.Empty;
                        }
                    }
                    row["zongdefen"] = fd.ZongDeFen;
                    row["pingguren"] = fd.PingGuRen;
                    //ID隐藏，双击触发事件
                    row["Id"] = record.Id;
                    dt.Rows.Add(row);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        #region GridView设置
        //GridView设置
        private void BindControl()
        {
            this.StatDate.DateTime = DateTime.Now.AddDays(-30).Date;
            this.EndDate.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 新增
        private void btn_Add_Click(object sender, EventArgs e)
        {
            AddFallRiskAssessment fra = new AddFallRiskAssessment(LoginUser, PatientInfo, string.Empty);
            fra.ShowDialog();
            if (fra.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }

        }
        #endregion

        #region 查询
        private void btn_Search_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 修改
        private void btn_Alter_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddFallRiskAssessment addForm = new AddFallRiskAssessment(LoginUser, PatientInfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }

        #endregion

        #region 打印
        private void btn_Print_Click(object sender, EventArgs e)
        {
            //多张打印
            FallRiskAssessmentPrint f = new FallRiskAssessmentPrint(LoginUser, PatientInfo, StatDate.DateTime, EndDate.DateTime);
            f.Show();
        }

        #endregion

        #region 删除
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    var ret2 = formService.DeleteNursingFormRecord(id);
                    MessageBox.Show(ret2.M);
                    if (ret2.Code == 1)
                    {
                        string dateStart = this.StatDate.Text;
                        string dateEnd = this.EndDate.Text;
                        BindInfo();
                    }
                }
            }
        }

        #endregion

        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Clicks == 2)
                {
                    string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                    AddFallRiskAssessment addForm = new AddFallRiskAssessment(LoginUser, PatientInfo, Id);
                    addForm.ShowDialog();
                    if (addForm.DialogResult == DialogResult.OK)
                    {
                        BindInfo();
                    }
                }
            }
        }
    }
}